<template>
  <div class="setpassword_wrap">
    <headTop :headClass="'white_header'">Set transaction password</headTop>
    <div class="set_wrap">
      <div class="info">
        <span>Cell-phone number</span>
        <span class="phone">{{phone ? HidePhone(phone) : phone}}</span>
      </div>
      <div class="info">
        <label for="code" class="code">
          <span>Verification Code</span>
          <input type="number" id="code" placeholder="Please input verification code." v-model="verify">
        </label>
        <span class="require_verify" :class="{'no_send' : !isSend, 'is_send' : isSend}" @click="sendCode">{{sendText}}</span>
      </div>
      <label for="password" class="info">
        <span>Transaction password</span>
        <input type="password" id="password" placeholder="Please set up6Bit digit transaction password" v-model="password">
      </label>
      <label for="repassword" class="info">
        <span>Confirm the password</span>
        <input type="password" id="repassword" placeholder="Please confirm the transaction password again." v-model="rePassword">
      </label>
    </div>
    <div class="confirm">
      <span class="public_btn" @click="submit">Sure</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'setPassword',
  data () {
    return {
      verify: '',
      password: '',
      rePassword: '',
      isSend: false,
      sendText: 'Get verification code',
      codeTime: 60
    };
  },
  props: [
    'phone'
  ],
  methods: {
    // Submission setting
    submit () {
      if (!this.verify) {
        this.Toast('Please input verification code.');
      } else if (!this.password) {
        this.Toast('Please enter the transaction password.');
      } else if (!/^\d{6}$/.test(this.password)) {
        this.Toast('Please enter6Bit digit transaction password');
      } else if (!this.rePassword) {
        this.Toast('Please enter confirmation password.');
      } else {
        this.setSubmit();
      }
    },
    // Send verification code
    sendCode () {
      if (this.isSend) {
        return false;
      } else {
        this.getCode();
      }
    },
    // Count down
    timer () {
      if (this.codeTime > 0) {
        this.codeTime--;
        this.sendText = `(${this.codeTime})s`;
        setTimeout(this.timer, 1000);
      } else {
        this.codeTime = 60;
        this.sendText = 'Recapture';
        this.isSend = false;
      }
    },
    // Sending verification code request
    getCode () {
      this.axios.post('index/sms/send', {
        phone: this.phone,
        type: 3
      }).then(({data}) => {
        if (data.status === 200) {
          this.isSend = true;
          this.timer();
        } else {
          this.Toast(data.message);
        }
      });
    },
    // Set transaction password submit
    setSubmit () {
      this.axios.post('index/users/payReset', {
        code: this.verify,
        pass: this.password,
        repass: this.rePassword
      }).then(({data}) => {
        if (data.status === 200) {
          this.Toast('The transaction password has been set successfully.');
          this.$router.go(-1);
        } else {
          this.Toast(data.message);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
  @import '../style/mixin.scss';
  .setpassword_wrap{
    padding-top: 108px;
    background-color: #fff;
    .set_wrap{
      width: 100%;
      padding: 0 54px;
      position: relative;
      margin-bottom: 100px;
      .info{
        width: 100%;
        height: 108px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 28px;
        color: #282828;
        border-bottom: 1px solid #e6e6e6;
        .code{
          width: 75%;
          height: 100%;
          display: flex;
          align-items: center;
          input{
            flex: 1;
            height: 100%;
          }
        }
        .phone{
          font-size: 26px;
        }
        .require_verify{
          font-size: 26px;
          color: $color;
          flex: 1;
          height: 100%;
          line-height: 108px;
          text-align: right;
          &.is_send{
            color: #666;
          }
        }
        input{
          flex: 1;
          height: 100%;
          text-align: right;
          &::placeholder{
            font-size: 26px;
            color: #999;
          }
        }
      }
      &::before{
        display: block;
        content: '';
        width: 100%;
        height: 20px;
        background-color: #f5f5f5;
        position: absolute;
        left: 0;
        top: -20px;
      }
    }
    .confirm{
      width: 100%;
      text-align: center;
    }
  }
</style>
